/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-04
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Navigation;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.NavigationService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设计器控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-04
 */
@Api(description = "设计器接口")
@RestController
@RequestMapping("/admin/navigation")
public class NavigationAdminController extends BaseController {

    @Autowired
    private NavigationService navigationService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询设计器
     *
     * @return
    */
    @ApiOperation("分页查询设计器")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tagIds", value = "标签", dataType = "String", paramType = "query")
    })
    public CommResult<Navigation> list(String name, Long[] tagIds, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("sortField", "orders");
        params.put("sortType","ASC");

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        if (name!=null) {
            params.put("name",name);
        }

        if (tagIds!=null && tagIds.length>0) {
            params.put("tagIds",tagIds);
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Navigation> list = navigationService.selectList(params);
        PageResult<Navigation> pageResult = new PageResult<Navigation>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条设计器
     *
     * @return
    */
    @ApiOperation("查询单条设计器")
    @GetMapping("/find/{id}")
    public CommResult<Navigation> find(@PathVariable String id) {
        Navigation result = navigationService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("navigation",result);
        return CommResult.success(data);

    }

    /**
     * 保存设计器
     *
     * @param navigation
     * @return
     */
    @ApiOperation("保存设计器")
    @PostMapping("/save")
    public CommResult<Navigation> save(@RequestBody Navigation navigation,Long [] tagIds) {
        Enterprise enterprise = enterpriseService.getCurrent();
        navigation.setEnterpriseId(enterprise.getId());
        navigation.setCreateDate(new Date());

        int affectCount = navigationService.save(navigation,tagIds);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        Navigation result = navigationService.findById(navigation.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除设计器
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除设计器", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除设计器,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Navigation> del(@PathVariable String ids) {

        navigationService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新设计器
     *
     * @param navigation
     * @return
     */
    @Log(desc = "更新设计器", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新设计器")
    @PostMapping("/update")
    public CommResult<Navigation> update(@RequestBody Navigation navigation,Long [] tagIds) {
        int affectCount = navigationService.update(navigation,tagIds);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Navigation result = navigationService.findById(navigation.getId());
        return CommResult.success(result);
    }

}
